#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
typedef long long LL;
typedef __int128 ML;
const int MAXN = 1e6+7;
int prime[MAXN], p_cnt;
int mu[MAXN];
bool not_p[MAXN];
void Init() {
    p_cnt = 0;
    mu[1] = 1;
    for (int i=2; i<MAXN; ++i) {
        if (!not_p[i]) {
            prime[p_cnt++] = i;
            mu[i] = -1;
        }
        for (int j=0; j<p_cnt&&i*prime[j]<MAXN; ++j) {
            not_p[i*prime[j]] = true;
            if (i%prime[j]==0) {
                mu[i*prime[j]] = 0;
                break;
            }
            mu[i*prime[j]] = -mu[i];
        }
    }
}

inline LL SA(ML n, ML p) {
    return n*(n+1)*(2*n+1)/6 % p;
}
inline LL SB(ML n, ML p) {
    ML ret = n*(n+1)/2 % p;
    ret = ret * ret % p;
    return ret;
}
inline LL ANS(ML n, ML x, ML p) {
    ML ret = (ML)mu[x] * x*x % p * x*x % p *((n+1)*SA(n/(x*x), p) - x*x*SB(n/(x*x), p)%p) % p;
    return ret;
}

int main() {
#ifdef hai
    freopen("in.txt", "r", stdin);
#endif
    Init();
    LL n, p;
    while (cin>>n>>p) {
        LL res = 0;
        LL len = sqrt(n);
        for (int x=1; x<=len; ++x) {
            res = (res + ANS(n, x, p)) % p;
        }
        if (res < 0) res += p;
        printf("%lld\n", res);
    }
    return 0;
}
